<!doctype html>
<html lang="en" data-color-mode="dark">
<head>
<meta charset="utf-8">
<title>Mitmproxy 备忘清单
 &#x26;  mitmproxy cheatsheet &#x26;  Quick Reference</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta description="Mitmproxy 是一个免费开源的交互式 HTTPS 代理。这是 mitmproxy 的快速参考备忘单。

入门，为开发人员分享快速参考备忘单。">
<meta keywords="mitmproxy,reference,Quick,Reference,cheatsheet,cheat,sheet">
<link rel="icon" href="data:image/svg+xml,%3Csvg%20viewBox%3D%220%200%2024%2024%22%20fill%3D%22none%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20height%3D%221em%22%20width%3D%221em%22%3E%20%3Cpath%20d%3D%22m21.66%2010.44-.98%204.18c-.84%203.61-2.5%205.07-5.62%204.77-.5-.04-1.04-.13-1.62-.27l-1.68-.4c-4.17-.99-5.46-3.05-4.48-7.23l.98-4.19c.2-.85.44-1.59.74-2.2%201.17-2.42%203.16-3.07%206.5-2.28l1.67.39c4.19.98%205.47%203.05%204.49%207.23Z%22%20fill%3D%22%23c9d1d9%22%2F%3E%20%3Cpath%20d%3D%22M15.06%2019.39c-.62.42-1.4.77-2.35%201.08l-1.58.52c-3.97%201.28-6.06.21-7.35-3.76L2.5%2013.28c-1.28-3.97-.22-6.07%203.75-7.35l1.58-.52c.41-.13.8-.24%201.17-.31-.3.61-.54%201.35-.74%202.2l-.98%204.19c-.98%204.18.31%206.24%204.48%207.23l1.68.4c.58.14%201.12.23%201.62.27Zm2.43-8.88c-.06%200-.12-.01-.19-.02l-4.85-1.23a.75.75%200%200%201%20.37-1.45l4.85%201.23a.748.748%200%200%201-.18%201.47Z%22%20fill%3D%22%23228e6c%22%20%2F%3E%20%3Cpath%20d%3D%22M14.56%2013.89c-.06%200-.12-.01-.19-.02l-2.91-.74a.75.75%200%200%201%20.37-1.45l2.91.74c.4.1.64.51.54.91-.08.34-.38.56-.72.56Z%22%20fill%3D%22%23228e6c%22%20%2F%3E%20%3C%2Fsvg%3E" type="image/svg+xml">
<link rel="stylesheet" href="../style/style.css">
<link rel="stylesheet" href="../style/katex.css">
</head>
<body><nav class="header-nav"><div class="max-container"><a href="../index.html" class="logo"><svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" height="1em" width="1em">
  <path d="m21.66 10.44-.98 4.18c-.84 3.61-2.5 5.07-5.62 4.77-.5-.04-1.04-.13-1.62-.27l-1.68-.4c-4.17-.99-5.46-3.05-4.48-7.23l.98-4.19c.2-.85.44-1.59.74-2.2 1.17-2.42 3.16-3.07 6.5-2.28l1.67.39c4.19.98 5.47 3.05 4.49 7.23Z" fill="#c9d1d9"></path>
  <path d="M15.06 19.39c-.62.42-1.4.77-2.35 1.08l-1.58.52c-3.97 1.28-6.06.21-7.35-3.76L2.5 13.28c-1.28-3.97-.22-6.07 3.75-7.35l1.58-.52c.41-.13.8-.24 1.17-.31-.3.61-.54 1.35-.74 2.2l-.98 4.19c-.98 4.18.31 6.24 4.48 7.23l1.68.4c.58.14 1.12.23 1.62.27Zm2.43-8.88c-.06 0-.12-.01-.19-.02l-4.85-1.23a.75.75 0 0 1 .37-1.45l4.85 1.23a.748.748 0 0 1-.18 1.47Z" fill="#228e6c"></path>
  <path d="M14.56 13.89c-.06 0-.12-.01-.19-.02l-2.91-.74a.75.75 0 0 1 .37-1.45l2.91.74c.4.1.64.51.54.91-.08.34-.38.56-.72.56Z" fill="#228e6c"></path>
</svg>
<span class="title">Quick Reference</span></a><div class="menu"><a href="javascript:void(0);" class="searchbtn" id="searchbtn"><svg xmlns="http://www.w3.org/2000/svg" height="1em" width="1em" viewBox="0 0 18 18">
  <path fill="currentColor" d="M17.71,16.29 L14.31,12.9 C15.4069846,11.5024547 16.0022094,9.77665502 16,8 C16,3.581722 12.418278,0 8,0 C3.581722,0 0,3.581722 0,8 C0,12.418278 3.581722,16 8,16 C9.77665502,16.0022094 11.5024547,15.4069846 12.9,14.31 L16.29,17.71 C16.4777666,17.8993127 16.7333625,18.0057983 17,18.0057983 C17.2666375,18.0057983 17.5222334,17.8993127 17.71,17.71 C17.8993127,17.5222334 18.0057983,17.2666375 18.0057983,17 C18.0057983,16.7333625 17.8993127,16.4777666 17.71,16.29 Z M2,8 C2,4.6862915 4.6862915,2 8,2 C11.3137085,2 14,4.6862915 14,8 C14,11.3137085 11.3137085,14 8,14 C4.6862915,14 2,11.3137085 2,8 Z"></path>
</svg><span>搜索</span><span>⌘K</span></a><a href="https://github.com/jaywcjlove/reference/blob/main/docs/mitmproxy.md" class="" target="__blank"><svg viewBox="0 0 36 36" fill="currentColor" height="1em" width="1em"><path d="m33 6.4-3.7-3.7a1.71 1.71 0 0 0-2.36 0L23.65 6H6a2 2 0 0 0-2 2v22a2 2 0 0 0 2 2h22a2 2 0 0 0 2-2V11.76l3-3a1.67 1.67 0 0 0 0-2.36ZM18.83 20.13l-4.19.93 1-4.15 9.55-9.57 3.23 3.23ZM29.5 9.43 26.27 6.2l1.85-1.85 3.23 3.23Z"></path><path fill="none" d="M0 0h36v36H0z"></path></svg><span>编辑</span></a><button id="darkMode" type="button"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" class="light" height="1em" width="1em">
  <path d="M6.995 12c0 2.761 2.246 5.007 5.007 5.007s5.007-2.246 5.007-5.007-2.246-5.007-5.007-5.007S6.995 9.239 6.995 12zM11 19h2v3h-2zm0-17h2v3h-2zm-9 9h3v2H2zm17 0h3v2h-3zM5.637 19.778l-1.414-1.414 2.121-2.121 1.414 1.414zM16.242 6.344l2.122-2.122 1.414 1.414-2.122 2.122zM6.344 7.759 4.223 5.637l1.415-1.414 2.12 2.122zm13.434 10.605-1.414 1.414-2.122-2.122 1.414-1.414z"></path>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 24 24" class="dark" height="1em" width="1em">
  <path d="M12 11.807A9.002 9.002 0 0 1 10.049 2a9.942 9.942 0 0 0-5.12 2.735c-3.905 3.905-3.905 10.237 0 14.142 3.906 3.906 10.237 3.905 14.143 0a9.946 9.946 0 0 0 2.735-5.119A9.003 9.003 0 0 1 12 11.807z"></path>
</svg>
</button><script src="../js/dark.js?v=1.5.5"></script><a href="https://github.com/jaywcjlove/reference" class="" target="__blank"><svg viewBox="0 0 16 16" fill="currentColor" height="1em" width="1em"><path d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.012 8.012 0 0 0 16 8c0-4.42-3.58-8-8-8z"></path></svg></a></div></div></nav><div class="wrap h1body-exist max-container"><header class="wrap-header h1wrap"><h1 id="mitmproxy-备忘清单"><svg xmlns="http://www.w3.org/2000/svg" height="1em" width="1em" viewBox="0 0 209 204">
<g fill="none" fill-rule="evenodd" transform="translate(5 5.644)">
<path fill="#F15A29" fill-rule="nonzero" d="M114,96.3562512 C114,104.088251 107.732,110.356251 100,110.356251 C92.268,110.356251 86,104.088251 86,96.3562512 C86,88.6242512 92.268,82.3562512 100,82.3562512 C107.732,82.3562512 114,88.6242512 114,96.3562512 Z"></path>
<path stroke="#F7941E" stroke-width="4" d="M119.5,96.3562512 C119.5,107.126251 110.77,115.856251 100,115.856251 C89.23,115.856251 80.5,107.126251 80.5,96.3562512 C80.5,85.5862512 89.23,76.8562512 100,76.8562512 C110.77,76.8562512 119.5,85.5862512 119.5,96.3562512 Z"></path>
<g stroke="currentColor" stroke-width="10">
<path d="M130,96.3562512 C130,112.925251 116.569,126.356251 100,126.356251 C83.431,126.356251 70,112.925251 70,96.3562512 C70,79.7872512 83.431,66.3562512 100,66.3562512 C116.569,66.3562512 130,79.7872512 130,96.3562512 Z"></path>
<path d="M167,24.3562512 C133.273,-4.80574879 84.314,-8.11674879 46.967,16.2392512 C9.62,40.5952512 -7.089,86.7342512 6,129.356251 L44,129.356251 C55.0860053,149.157715 75.6966688,161.743751 98.3755043,162.561006 C121.05434,163.378261 142.517468,152.308398 155,133.356251 M0,96.3562512 L70,96.3562512"></path></g>
<path fill="#F7941E" fill-rule="nonzero" d="M164,125.356251 C164,128.66996 161.313708,131.356251 158,131.356251 C154.686292,131.356251 152,128.66996 152,125.356251 C152,122.042543 154.686292,119.356251 158,119.356251 C161.313708,119.356251 164,122.042543 164,125.356251 Z"></path>
<path stroke="currentColor" stroke-width="3.2" d="M20,10 C20,15.523 15.523,20 10,20 C4.477,20 0,15.523 0,10 C0,4.477 4.477,0 10,0 C15.523,0 20,4.477 20,10 Z" transform="translate(148 115.356)"></path>
<path fill="#F7941E" fill-rule="nonzero" d="M179,31.3562512 C179,34.6699597 176.313708,37.3562512 173,37.3562512 C169.686292,37.3562512 167,34.6699597 167,31.3562512 C167,28.0425427 169.686292,25.3562512 173,25.3562512 C176.313708,25.3562512 179,28.0425427 179,31.3562512 Z"></path>
<g stroke="currentColor" transform="translate(33 21.356)">
<path stroke-width="3.2" d="M20,10 C20,15.523 15.523,20 10,20 C4.477,20 0,15.523 0,10 C0,4.477 4.477,0 10,0 C15.523,0 20,4.477 20,10 Z" transform="translate(130)"></path>
<path stroke-width="10" d="M0,147 C33.727,176.162 82.686,179.473 120.033,155.117 C157.38,130.761 174.089,84.622 161,42 L123,42 C111.913995,22.1985361 91.3033312,9.61250069 68.6244957,8.79524536 C45.9456602,7.97799002 24.4825319,19.0478528 12,38 M167,75 L97,75"></path></g>
<path fill="#F7941E" fill-rule="nonzero" d="M48,67.3562512 C48,70.6699597 45.3137085,73.3562512 42,73.3562512 C38.6862915,73.3562512 36,70.6699597 36,67.3562512 C36,64.0425427 38.6862915,61.3562512 42,61.3562512 C45.3137085,61.3562512 48,64.0425427 48,67.3562512 Z"></path>
<path stroke="currentColor" stroke-width="3.2" d="M20,10 C20,15.523 15.523,20 10,20 C4.477,20 0,15.523 0,10 C0,4.477 4.477,0 10,0 C15.523,0 20,4.477 20,10 Z" transform="translate(32 57.356)"></path>
<path fill="#F7941E" fill-rule="nonzero" d="M33,161.356251 C33,163.499845 31.8564065,165.480607 30,166.552404 C28.1435936,167.624201 25.8564064,167.624201 24,166.552404 C22.1435935,165.480607 21,163.499845 21,161.356251 C21,158.042543 23.6862915,155.356251 27,155.356251 C30.3137085,155.356251 33,158.042543 33,161.356251 L33,161.356251 Z"></path>
<path stroke="currentColor" stroke-width="3.2" d="M20,10 C20,15.523 15.523,20 10,20 C4.477,20 0,15.523 0,10 C0,4.477 4.477,0 10,0 C15.523,0 20,4.477 20,10 Z" transform="translate(17 151.356)"></path>
</g>
</svg><a aria-hidden="true" tabindex="-1" href="#mitmproxy-备忘清单"><span class="icon icon-link"></span></a>Mitmproxy 备忘清单</h1><div class="wrap-body">
<p><a href="https://mitmproxy.org/">Mitmproxy</a> 是一个免费开源的交互式 HTTPS 代理。这是 mitmproxy 的快速参考备忘单。</p>
</div></header><div class="menu-tocs"><div class="menu-btn"><svg aria-hidden="true" fill="currentColor" height="1em" width="1em" viewBox="0 0 16 16" version="1.1" data-view-component="true">
  <path fill-rule="evenodd" d="M2 4a1 1 0 100-2 1 1 0 000 2zm3.75-1.5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zm0 5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zm0 5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zM3 8a1 1 0 11-2 0 1 1 0 012 0zm-1 6a1 1 0 100-2 1 1 0 000 2z"></path>
</svg></div><div class="menu-modal"><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#入门">入门</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#使用">使用</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#移动">移动</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#安装">安装</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#代理模式">代理模式</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#复制到剪贴板">复制到剪贴板</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#保存到文件">保存到文件</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#流视图">流（视图）</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#常见的快捷键">常见的快捷键</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#全局键绑定">全局键绑定</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#代理选项">代理选项</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#mitmproxy-过滤器">Mitmproxy 过滤器</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#过滤器">过滤器</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#运算符">运算符</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#表达式">表达式</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#流选择器">流选择器</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#示例">示例</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#mitmproxy-脚本">Mitmproxy 脚本</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#custom-response">Custom response</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#add-header">Add header</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#另见">另见</a></div></div><div class="h1wrap-body"><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="入门"><a aria-hidden="true" tabindex="-1" href="#入门"><span class="icon icon-link"></span></a>入门</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="使用"><a aria-hidden="true" tabindex="-1" href="#使用"><span class="icon icon-link"></span></a>使用</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2-->


















































<table><thead><tr><th align="left">选项</th><th>范例</th><th>描述</th></tr></thead><tbody><tr><td align="left"><code>-p</code></td><td>mitmproxy -p 8001</td><td>在端口 <code>8001</code> 上启动代理</td></tr><tr><td align="left"><code>-m</code></td><td>mitmproxy -p 8001 -m reverse:<a href="http://127.0.0.1:4000">http://127.0.0.1:4000</a></td><td><code>8001</code> 端口反向代理到4000端口</td></tr><tr><td align="left"><code>-w</code></td><td>mitmproxy -p 8001 -w traffic.mitm</td><td>流到达时流向文件</td></tr><tr><td align="left"><code>-r</code></td><td>mitmproxy -r traffic.mitm</td><td>从文件中读取流</td></tr><tr><td align="left"><code>-C</code></td><td>mitmproxy -C traffic.mitm</td><td>从保存的文件重放客户端请求</td></tr><tr><td align="left"><code>-S</code></td><td>mitmproxy -S traffic.mitm</td><td>从保存的文件重放服务器响应</td></tr><tr><td align="left"><code>-s</code></td><td>mitmproxy -s myScript.py</td><td>执行脚本</td></tr><tr><td align="left"><code>-h</code></td><td>mitmproxy -h</td><td><code>mitmproxy</code> 快速帮助</td></tr></tbody></table>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="移动"><a aria-hidden="true" tabindex="-1" href="#移动"><span class="icon icon-link"></span></a>移动</h3><div class="wrap-body">
<pre class="language-markdown"><code class="language-markdown code-highlight"><span class="code-line">        k                 Ctrl b
</span><span class="code-line">        ▲                   ▲▲
</span><span class="code-line">        │                   ││
</span><span class="code-line">h ◀ ─── + ─── ▶ l           ││ page
</span><span class="code-line">        │                   ││
</span><span class="code-line">        ▼                   ▼▼
</span><span class="code-line">        j             Ctrl f / Space 
</span></code></pre>
<hr>





























<table class="shortcuts"><thead><tr><th align="left">:---</th><th>---</th></tr></thead><tbody><tr><td align="left"><code>h</code>, <code>j</code>, <code>k</code> ,<code>l</code></td><td>左、下、上、右</td></tr><tr><td align="left"><code>Ctrl</code> <code>b</code></td><td>向上翻页</td></tr><tr><td align="left"><code>Space</code> / <code>Ctrl</code> <code>f</code></td><td>向下翻页</td></tr><tr><td align="left"><code>g</code> / <code>G</code></td><td>转到开头/结尾</td></tr><tr><td align="left"><code>Arrows</code></td><td>上下左右</td></tr></tbody></table>
<!--rehype:className=shortcuts-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="安装"><a aria-hidden="true" tabindex="-1" href="#安装"><span class="icon icon-link"></span></a>安装</h3><div class="wrap-body">
<ul>
<li><a href="https://docs.mitmproxy.org/stable/">mitmproxy 文档</a> <em>(mitmproxy.org)</em></li>
<li><a href="https://github.com/mitmproxy/mitmproxy">mitmproxy 开源仓库</a> <em>(github.com)</em></li>
</ul>
<hr>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">$ brew <span class="token function">install</span> mitmproxy <span class="token comment"># macOS</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="代理模式"><a aria-hidden="true" tabindex="-1" href="#代理模式"><span class="icon icon-link"></span></a>代理模式</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2-->

























<table class="style-list"><thead><tr><th align="left">Argument</th><th>Effect</th></tr></thead><tbody><tr><td align="left"><code>-R REVERSE_PROXY</code>, <code>--reverse REVERSE_PROXY</code></td><td>将所有请求转发到上游 HTTP 服务器：<code>http[s]://host[:port]</code>。客户端始终可以通过 <code>HTTPS</code> 和 <code>HTTP</code> 进行连接，与服务器的连接由指定的方案决定</td></tr><tr><td align="left"><code>--socks</code></td><td>设置 <code>SOCKS5</code> 代理模式</td></tr><tr><td align="left"><code>-T</code>, <code>--transparent</code></td><td>设置透明代理模式</td></tr><tr><td align="left"><code>-U UPSTREAM_PROXY</code>, <code>--upstream UPSTREAM_PROXY</code></td><td>将所有请求转发到上游代理服务器：<code>http://host[:port]</code></td></tr></tbody></table>
<!--rehype:className=style-list-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="复制到剪贴板"><a aria-hidden="true" tabindex="-1" href="#复制到剪贴板"><span class="icon icon-link"></span></a>复制到剪贴板</h3><div class="wrap-body">
<p>命令语法：</p>
<pre><code class="code-highlight"><span class="code-line">:export.clip format flow
</span></code></pre>
<p>例子：</p>





























<table class="style-list"><thead><tr><th>描述</th><th>命令示例</th></tr></thead><tbody><tr><td>1. 复制为 curl 命令</td><td><code>:export.clip curl @focus</code></td></tr><tr><td>2. 复制为 httpie</td><td><code>:export.clip httpie @focus</code></td></tr><tr><td>3. 复制为原始文件</td><td><code>:export.clip raw @focus</code></td></tr><tr><td>4. 复制为原始 HTTP 请求</td><td><code>:export.clip raw_request @focus</code></td></tr><tr><td>5. 复制为原始 HTTP 响应</td><td><code>:export.clip raw_response @focus</code></td></tr></tbody></table>
<!--rehype:className=style-list-->
<p>将流程导出到系统剪贴板</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="保存到文件"><a aria-hidden="true" tabindex="-1" href="#保存到文件"><span class="icon icon-link"></span></a>保存到文件</h3><div class="wrap-body">
<p>命令语法：</p>
<pre><code class="code-highlight"><span class="code-line">:export.file format flow path
</span></code></pre>
<p>例子：</p>





























<table class="style-list"><thead><tr><th>描述</th><th>命令示例</th></tr></thead><tbody><tr><td>1. 导出到 <code>/tmp/a.curl</code></td><td><code>:export.file curl @focus /tmp/a.curl</code></td></tr><tr><td>2. 导出到 <code>/tmp/a.httpie</code></td><td><code>:export.file httpie @focus /tmp/a.httpie</code></td></tr><tr><td>3. 导出到 <code>/tmp/a.raw</code></td><td><code>:export.file raw @focus  /tmp/a.raw</code></td></tr><tr><td>4. 导出到 <code>/tmp/a.request</code></td><td><code>:export.file raw_request @focus /tmp/a.request</code></td></tr><tr><td>5. 导出到 <code>/tmp/a.response</code></td><td><code>:export.file raw_response @focus /tmp/a.response</code></td></tr></tbody></table>
<!--rehype:className=style-list-->
<p>将流程导出到系统剪贴板</p>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="流视图"><a aria-hidden="true" tabindex="-1" href="#流视图"><span class="icon icon-link"></span></a>流（视图）</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->





































































































<table class="shortcuts"><thead><tr><th align="left">:---</th><th>---</th></tr></thead><tbody><tr><td align="left"><code>A</code></td><td>恢复所有拦截的流</td></tr><tr><td align="left"><code>D</code></td><td>重复流</td></tr><tr><td align="left"><code>F</code></td><td>设置焦点跟随</td></tr><tr><td align="left"><code>L</code></td><td>从文件加载流</td></tr><tr><td align="left"><code>M</code></td><td>切换查看标记流</td></tr><tr><td align="left"><code>S</code></td><td>开始服务器回放</td></tr><tr><td align="left"><code>U</code></td><td>取消设置所有标记</td></tr><tr><td align="left"><code>V</code></td><td>还原对此流的更改</td></tr><tr><td align="left"><code>X</code></td><td>杀死这个流</td></tr><tr><td align="left"><code>Z</code></td><td>清除所有未显示的流</td></tr><tr><td align="left"><code>a</code></td><td>恢复此拦截流</td></tr><tr><td align="left"><code>b</code></td><td>将响应主体保存到文件</td></tr><tr><td align="left"><code>d</code></td><td>从视图中删除流</td></tr><tr><td align="left"><code>e</code></td><td>将此流导出到文件</td></tr><tr><td align="left"><code>f</code></td><td>设置视图过滤器</td></tr><tr><td align="left"><code>m</code></td><td>在此流程上切换标记</td></tr><tr><td align="left"><code>n</code></td><td>创建新流程</td></tr><tr><td align="left"><code>o</code></td><td>设置流列表顺序</td></tr><tr><td align="left"><code>r</code></td><td>重播此流程</td></tr><tr><td align="left"><code>v</code></td><td>反向流列表顺序</td></tr><tr><td align="left"><code>w</code></td><td>将列出的流程保存到文件</td></tr><tr><td align="left"><code>|</code></td><td>在此流上运行脚本</td></tr><tr><td align="left"><code>Ctrl</code> <code>l</code></td><td>将剪辑发送到剪贴板</td></tr></tbody></table>
<!--rehype:className=shortcuts-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="常见的快捷键"><a aria-hidden="true" tabindex="-1" href="#常见的快捷键"><span class="icon icon-link"></span></a>常见的快捷键</h3><div class="wrap-body">









































<table class="shortcuts"><thead><tr><th align="left">:---</th><th>---</th></tr></thead><tbody><tr><td align="left"><code>q</code></td><td>返回/退出</td></tr><tr><td align="left"><code>z</code></td><td>清除流列表</td></tr><tr><td align="left"><code>:</code></td><td>命令提示符</td></tr><tr><td align="left"><code>E</code></td><td>查看事件日志</td></tr><tr><td align="left"><code>O</code></td><td>查看选项</td></tr><tr><td align="left"><code>r</code></td><td>重播此流程</td></tr><tr><td align="left"><code>Tab</code></td><td>下一个</td></tr><tr><td align="left"><code>Enter</code></td><td>选择</td></tr></tbody></table>
<!--rehype:className=shortcuts-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="全局键绑定"><a aria-hidden="true" tabindex="-1" href="#全局键绑定"><span class="icon icon-link"></span></a>全局键绑定</h3><div class="wrap-body">

























































<table class="shortcuts"><thead><tr><th align="left">:---</th><th>---</th></tr></thead><tbody><tr><td align="left"><code>-</code></td><td>循环到下一个布局</td></tr><tr><td align="left"><code>?</code></td><td>查看帮助</td></tr><tr><td align="left"><code>B</code></td><td>启动附加的浏览器</td></tr><tr><td align="left"><code>C</code></td><td>查看命令</td></tr><tr><td align="left"><code>I</code></td><td>切换拦截</td></tr><tr><td align="left"><code>K</code></td><td>查看按键绑定</td></tr><tr><td align="left"><code>P</code></td><td>查看流程详细信息</td></tr><tr><td align="left"><code>Q</code></td><td>立即退出</td></tr><tr><td align="left"><code>W</code></td><td>流式传输到文件</td></tr><tr><td align="left"><code>i</code></td><td>设置拦截</td></tr><tr><td align="left"><code>Ctrl</code> <code>right</code></td><td>聚焦下一个布局窗格</td></tr><tr><td align="left"><code>Shift</code> <code>tab</code></td><td>聚焦下一个布局窗格</td></tr></tbody></table>
<!--rehype:className=shortcuts-->
</div></div></div><div class="wrap h3body-not-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="代理选项"><a aria-hidden="true" tabindex="-1" href="#代理选项"><span class="icon icon-link"></span></a>代理选项</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2-->

















































<table class="style-list"><thead><tr><th align="left">:---</th><th>---</th></tr></thead><tbody><tr><td align="left"><code>-b ADDR</code>, <code>--bind-address ADDR</code></td><td>将代理绑定到的地址（默认为所有接口）</td></tr><tr><td align="left"><code>-I HOST</code>, <code>--ignore HOST</code></td><td>忽略主机并转发所有流量而不对其进行处理。在透明模式下，建议使用 IP 地址（范围），而不是主机名。在常规模式下，仅忽略 SSL 流量并应使用主机名。提供的值被解释为正则表达式并匹配 ip 或主机名。可以多次通过</td></tr><tr><td align="left"><code>--tcp HOST</code></td><td>与模式匹配的所有主机的通用 TCP SSL 代理模式。类似于 <code>--ignore</code>，但 SSL 连接被拦截。通信内容以详细模式打印到日志中</td></tr><tr><td align="left"><code>-n</code>, <code>--no-server</code></td><td>不要启动代理服务器。用于离线分析以前捕获的流</td></tr><tr><td align="left"><code>-p PORT</code>, <code>--port PORT</code></td><td>代理服务端口。默认值：<code>8080</code></td></tr><tr><td align="left"><code>--http2</code>, <code>--no-http2</code></td><td>显式启用/禁用 <code>HTTP/2</code> 支持。默认情况下禁用，直到主要网站正确实施规范。默认值将在未来版本中更改</td></tr><tr><td align="left"><code>--no-websocket</code>, <code>--websocket</code></td><td>显式启用/禁用 <code>WebSocket</code> 支持。默认启用</td></tr><tr><td align="left"><code>--raw-tcp</code>, <code>--no-raw-tcp</code></td><td>显式启用/禁用实验性原始 <code>TCP</code> 支持。默认情况下禁用。默认值将在未来版本中更改</td></tr><tr><td align="left"><code>--spoof-source-address</code></td><td>使用客户端的 IP 进行服务器端连接。与 <code>–upstream-bind-address</code> 结合使用以欺骗固定源地址</td></tr><tr><td align="left"><code>--upstream-bind-address UPSTREAM_BIND_ADDRESS</code></td><td>将上游请求绑定到的地址（默认为无）</td></tr></tbody></table>
<!--rehype:className=style-list-->
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="mitmproxy-过滤器"><a aria-hidden="true" tabindex="-1" href="#mitmproxy-过滤器"><span class="icon icon-link"></span></a>Mitmproxy 过滤器</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="过滤器"><a aria-hidden="true" tabindex="-1" href="#过滤器"><span class="icon icon-link"></span></a>过滤器</h3><div class="wrap-body">













<table class="shortcuts"><thead><tr><th align="left">:---</th><th>---</th></tr></thead><tbody><tr><td align="left"><code>f</code></td><td>设置视图过滤器 <em>(在流视图页面上)</em></td></tr></tbody></table>
<!--rehype:className=shortcuts-->
<hr>
<ul>
<li><a href="./regex.html">RegEX 备忘清单</a> <em>(jaywcjlove.github.io)</em></li>
</ul>
<p>正则表达式是 Python 风格的，可以指定为带引号的字符串</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="运算符"><a aria-hidden="true" tabindex="-1" href="#运算符"><span class="icon icon-link"></span></a>运算符</h3><div class="wrap-body">

























<table><thead><tr><th align="left">:---</th><th>---</th></tr></thead><tbody><tr><td align="left"><code>!</code></td><td>一元非</td></tr><tr><td align="left"><code>&#x26;</code></td><td>和</td></tr><tr><td align="left"><code>|</code></td><td>或者</td></tr><tr><td align="left"><code>(...)</code></td><td>分组</td></tr></tbody></table>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="表达式"><a aria-hidden="true" tabindex="-1" href="#表达式"><span class="icon icon-link"></span></a>表达式</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->





































































































<table><thead><tr><th align="left">:---</th><th>---</th></tr></thead><tbody><tr><td align="left"><code>~a</code></td><td>响应匹配资源：CSS、Javascript、Flash、图像。</td></tr><tr><td align="left"><code>~b</code> <code>regex</code></td><td>主体 Body</td></tr><tr><td align="left"><code>~bq</code> <code>regex</code></td><td>请求正文</td></tr><tr><td align="left"><code>~bs</code> <code>regex</code></td><td>响应体</td></tr><tr><td align="left"><code>~c</code> <code>int</code></td><td>HTTP 响应代码</td></tr><tr><td align="left"><code>~d</code> <code>regex</code></td><td>域</td></tr><tr><td align="left"><code>~dst</code> <code>regex</code></td><td>匹配目标地址</td></tr><tr><td align="left"><code>~e</code></td><td>匹配错误</td></tr><tr><td align="left"><code>~h</code> <code>regex</code></td><td>标头</td></tr><tr><td align="left"><code>~hq</code> <code>regex</code></td><td>请求头</td></tr><tr><td align="left"><code>~hs</code> <code>regex</code></td><td>响应头</td></tr><tr><td align="left"><code>~http</code></td><td>匹配 HTTP 流</td></tr><tr><td align="left"><code>~m</code> <code>regex</code></td><td>方法</td></tr><tr><td align="left"><code>~marked</code></td><td>匹配标记流</td></tr><tr><td align="left"><code>~q</code></td><td>匹配请求无响应</td></tr><tr><td align="left"><code>~s</code></td><td>匹配响应</td></tr><tr><td align="left"><code>~src</code> <code>regex</code></td><td>匹配源地址</td></tr><tr><td align="left"><code>~t</code> <code>regex</code></td><td>内容类型标头</td></tr><tr><td align="left"><code>~tcp</code></td><td>匹配 TCP 流</td></tr><tr><td align="left"><code>~tq</code> <code>regex</code></td><td>请求 Content-Type 标头</td></tr><tr><td align="left"><code>~ts</code> <code>regex</code></td><td>响应内容类型标头</td></tr><tr><td align="left"><code>~u</code> <code>regex</code></td><td>网址</td></tr><tr><td align="left"><code>~websocket</code></td><td>匹配 WebSocket 流（和 HTTP-WebSocket 握手流）</td></tr></tbody></table>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="流选择器"><a aria-hidden="true" tabindex="-1" href="#流选择器"><span class="icon icon-link"></span></a>流选择器</h3><div class="wrap-body">
<p>表达式</p>

































<table><thead><tr><th align="left">:---</th><th>---</th></tr></thead><tbody><tr><td align="left"><code>@all</code></td><td>所有流程</td></tr><tr><td align="left"><code>@focus</code></td><td>目前关注的流程</td></tr><tr><td align="left"><code>@shown</code></td><td>当前显示的所有流程</td></tr><tr><td align="left"><code>@hidden</code></td><td>当前隐藏的所有流程</td></tr><tr><td align="left"><code>@marked</code></td><td>所有标记流</td></tr><tr><td align="left"><code>@unmarked</code></td><td>所有未标记的流</td></tr></tbody></table>
<p>mitmproxy 有一组方便的流选择器，可以在当前视图上操作</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="示例"><a aria-hidden="true" tabindex="-1" href="#示例"><span class="icon icon-link"></span></a>示例</h3><div class="wrap-body">
<p>包含“google.com”的网址</p>
<pre><code class="code-highlight"><span class="code-line">google\.com
</span></code></pre>
<p>正文中包含字符串“test”的请求</p>
<pre><code class="code-highlight"><span class="code-line">~q ~b test
</span></code></pre>
<p>除了带有 text/html 内容类型的请求之外的任何内容：</p>
<pre><code class="code-highlight"><span class="code-line">!(~q &#x26; ~t "text/html")
</span></code></pre>
<p>替换请求中的整个 GET 字符串（需要引号才能使其工作）：</p>
<pre><code class="code-highlight"><span class="code-line">":~q ~m GET:.*:/replacement.html"
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="mitmproxy-脚本"><a aria-hidden="true" tabindex="-1" href="#mitmproxy-脚本"><span class="icon icon-link"></span></a>Mitmproxy 脚本</h2><div class="wrap-body">
<!--rehype:body-class=cols-2-->
</div></div><div class="h2wrap-body cols-2"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="custom-response"><a aria-hidden="true" tabindex="-1" href="#custom-response"><span class="icon icon-link"></span></a>Custom response</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token keyword">from</span> mitmproxy <span class="token keyword">import</span> http
</span><span class="code-line"><span class="token keyword">def</span> <span class="token function">request</span><span class="token punctuation">(</span>flow<span class="token punctuation">:</span> http<span class="token punctuation">.</span>HTTPFlow<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> <span class="token boolean">None</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">if</span> flow<span class="token punctuation">.</span>request<span class="token punctuation">.</span>pretty_url <span class="token operator">==</span> <span class="token string">"http://example.com/path"</span><span class="token punctuation">:</span>
</span><span class="code-line">        flow<span class="token punctuation">.</span>response <span class="token operator">=</span> http<span class="token punctuation">.</span>HTTPResponse<span class="token punctuation">.</span>make<span class="token punctuation">(</span>
</span><span class="code-line">            <span class="token number">200</span><span class="token punctuation">,</span>  <span class="token comment"># (optional) status code</span>
</span><span class="code-line">            <span class="token string">b"Hello World"</span><span class="token punctuation">,</span>  <span class="token comment"># (optional) content</span>
</span><span class="code-line">            <span class="token punctuation">{</span><span class="token string">"Content-Type"</span><span class="token punctuation">:</span> <span class="token string">"text/html"</span><span class="token punctuation">}</span>  <span class="token comment"># (optional) headers</span>
</span><span class="code-line">        <span class="token punctuation">)</span>
</span></code></pre>
<p>从代理发送回复而不向远程服务器发送任何数据</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="add-header"><a aria-hidden="true" tabindex="-1" href="#add-header"><span class="icon icon-link"></span></a>Add header</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token keyword">class</span> <span class="token class-name">AddHeader</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">        self<span class="token punctuation">.</span>num <span class="token operator">=</span> <span class="token number">0</span>
</span><span class="code-line">    <span class="token keyword">def</span> <span class="token function">response</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> flow<span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">        self<span class="token punctuation">.</span>num <span class="token operator">=</span> self<span class="token punctuation">.</span>num <span class="token operator">+</span> <span class="token number">1</span>
</span><span class="code-line">        flow<span class="token punctuation">.</span>response<span class="token punctuation">.</span>headers<span class="token punctuation">[</span><span class="token string">"count"</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token builtin">str</span><span class="token punctuation">(</span>self<span class="token punctuation">.</span>num<span class="token punctuation">)</span>
</span><span class="code-line">addons <span class="token operator">=</span> <span class="token punctuation">[</span>
</span><span class="code-line">    AddHeader<span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">]</span>
</span></code></pre>
<p>为每个响应添加一个 HTTP 标头</p>
</div></div></div></div></div><div class="wrap h2body-not-exist"><div class="wrap-header h2wrap"><h2 id="另见"><a aria-hidden="true" tabindex="-1" href="#另见"><span class="icon icon-link"></span></a>另见</h2><div class="wrap-body">
<ul>
<li><a href="https://github.com/mitmproxy/mitmproxy/tree/master/examples/addons">mitmproxy addons</a> <em>(github.com)</em></li>
<li><a href="https://docs.mitmproxy.org/stable/">mitmproxy 文档</a> <em>(mitmproxy.org)</em></li>
<li><a href="https://github.com/mitmproxy/mitmproxy">mitmproxy 开源仓库</a> <em>(github.com)</em></li>
<li><a href="https://www.stut-it.net/blog/2017/mitmproxy-cheatsheet.html">mitmproxy 备忘清单</a> <em>(stut-it.net)</em></li>
</ul>
</div></div><div class="h2wrap-body"></div></div></div><script src="https://giscus.app/client.js" data-repo="jaywcjlove/reference" data-repo-id="R_kgDOID2-Mw" data-category="Q&#x26;A" data-category-id="DIC_kwDOID2-M84CS5wo" data-mapping="pathname" data-strict="0" data-reactions-enabled="1" data-emit-metadata="0" data-input-position="bottom" data-theme="dark" data-lang="zh-CN" crossorigin="anonymous" async></script><div class="giscus"></div></div><footer class="footer-wrap"><footer class="max-container">© 2022 <a href="https://wangchujiang.com/#/app" target="_blank">Kenny Wang</a>.</footer></footer><script src="../data.js?v=1.5.5" defer></script><script src="../js/fuse.min.js?v=1.5.5" defer></script><script src="../js/main.js?v=1.5.5" defer></script><div id="mysearch"><div class="mysearch-box"><div class="mysearch-input"><div><svg xmlns="http://www.w3.org/2000/svg" height="1em" width="1em" viewBox="0 0 18 18">
  <path fill="currentColor" d="M17.71,16.29 L14.31,12.9 C15.4069846,11.5024547 16.0022094,9.77665502 16,8 C16,3.581722 12.418278,0 8,0 C3.581722,0 0,3.581722 0,8 C0,12.418278 3.581722,16 8,16 C9.77665502,16.0022094 11.5024547,15.4069846 12.9,14.31 L16.29,17.71 C16.4777666,17.8993127 16.7333625,18.0057983 17,18.0057983 C17.2666375,18.0057983 17.5222334,17.8993127 17.71,17.71 C17.8993127,17.5222334 18.0057983,17.2666375 18.0057983,17 C18.0057983,16.7333625 17.8993127,16.4777666 17.71,16.29 Z M2,8 C2,4.6862915 4.6862915,2 8,2 C11.3137085,2 14,4.6862915 14,8 C14,11.3137085 11.3137085,14 8,14 C4.6862915,14 2,11.3137085 2,8 Z"></path>
</svg><input id="mysearch-input" type="search" placeholder="搜索" autocomplete="off"><div class="mysearch-clear"></div></div><button id="mysearch-close" type="button">搜索</button></div><div class="mysearch-result"><div id="mysearch-menu"></div><div id="mysearch-content"></div></div></div></div></body>
</html>
